package cn.kgc.ebookvue02.service;

import cn.kgc.ebookvue02.mapper.EntryMapper;
import cn.kgc.ebookvue02.pojo.Entry;
import cn.kgc.ebookvue02.pojo.EntryExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-18 22:02
 */
@Service("entryService")
public class EntryServiceImpl implements  EntryService {

    @Resource
    EntryMapper entryMapper;
    @Override
    public PageInfo<Entry> selectEntryPageinfo(Integer pageNum, Integer pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize);
        EntryExample example=new EntryExample();
        EntryExample.Criteria criteria = example.createCriteria();

        if(categoryId==0) {
            List<Entry> entries = entryMapper.selectByExample(null);
            PageInfo<Entry> pageInfo=new PageInfo<>(entries);
            return pageInfo;
        }else{
            criteria.andCategoryidEqualTo(categoryId);
            List<Entry> entries = entryMapper.selectByExample(example);
            PageInfo<Entry> pageInfo=new PageInfo<>(entries);
            return pageInfo;
        }
    }

    @Override
    public Integer addEntry(Entry entry) {
        return entryMapper.insert(entry);
    }

    @Override
    public Integer deleteEntry(Integer id) {
        return entryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Entry selectById(Integer id) {
        return entryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateEntry(Entry entry) {
        return entryMapper.updateByPrimaryKeySelective(entry);
    }
}
